package com.msz.controller.rest;

import com.msz.domain.User;
import com.msz.service.UserService;
import com.msz.util.JwtUtils;
import com.msz.util.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author hx
 * @since 2021/12/2 7:00 下午
 */

@Controller
@RequestMapping(value = "/api/user", produces = {"application/json;charset=UTF-8"})
@Api(tags = "用户管理")
public class UserRestController {
    @Autowired
    private UserService userService;

    //========================REST API CRUD=================================

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询用户")
    public R getUser(
            @ApiParam("用户id") @PathVariable("id") Integer id) {
        User user = userService.queryUserById(id);
        return R.result(20000,"查询成功",user);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation("删除用户")
    public R deleteUser(@ApiParam("用户id")@PathVariable("id") Integer id) {
        Boolean flag = userService.deleteUserById(id);
        if (flag) {
            return R.result(20000,"删除用户成功");
        }else {
            return R.result(30000,"删除用户失败");
        }
    }

    @RequestMapping( method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation("修改用户")
    public R putUser(@RequestBody User user) {
        Boolean flag = userService.updateUserById(user);
        if (flag) {
            return R.result(20000,"修改用户成功");
        }else {
            return R.result(30000,"修改用户失败");
        }
    }

    @RequestMapping( method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("添加用户")
    public R insertUser(@RequestBody User user) {
        Boolean flag = userService.insertUser(user);
        if (flag) {
            return R.result(20000,"添加用户成功");
        }else {
            return R.result(30000,"添加用户失败");
        }
    }


}
